//
//  NKLeftViewController.m
//  NewKY
//
//  Created by Dee on 2016/11/12.
//  Copyright © 2016年 Dee. All rights reserved.
//

#import "NKLeftViewController.h"
#import "NKMainNavigation.h"
#import "UIViewController+LMSideBarController.h"

@interface NKLeftViewController ()<UITableViewDelegate,UITableViewDataSource>



@end

@implementation NKLeftViewController{
    
    NSArray<NSDictionary *> *_menuArray;
    
    NSArray<NSDictionary *> *_normalArray;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    #pragma mark - 临时帐号
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSInteger status = [[userDefaults objectForKey:@"USER_STATUS"]intValue];
        //若已经登录,则不创建测试帐号
    if(!status){
        
        [userDefaults setObject:@"123456" forKey:[@"phone="stringByAppendingString:@"123456"]];
        [userDefaults setObject:@"qwer" forKey:[@"password="stringByAppendingString:@"123456"]];
        [userDefaults synchronize];
        [userDefaults setObject:@"123456" forKey:@"NOW_USER_PHONE"];
        
        NSLog(@"测试用户:%@ 密码:%@",@"123456",@"qwer");
        
    }
    
    
    //头部视图
    _headerView = [[[NSBundle mainBundle]loadNibNamed:@"NKLeftHeaderView" owner:nil options:nil]lastObject];
    [self.tableView setTableHeaderView:_headerView];
    
    _headerView.pushBlock = ^(NSString *className,NSString *title){
        
        NKMainNavigation *nav = (NKMainNavigation *)self.sideBarController.contentViewController;
        
            //点击用户信息时
        
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        NSInteger status = [[userDefaults objectForKey:@"USER_STATUS"]intValue];
        
            //点击头像时根据状态区分
        if([className isEqualToString:@"NKAccountViewController"]){
            
            if(!status){
                return ;
            }
            
            [nav showOtherViewController:className title:title];
            
        }else{
            
            UIViewController *vc = [NSClassFromString(className) new];
            [nav pushViewController:vc animated:NO];
            
        }
        
        //手动隐藏侧滑
        [self.sideBarController hideMenuViewController:YES];

    };
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self loadData];
    
}

#pragma mark - tableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    NSInteger status = [[userDefaults objectForKey:@"USER_STATUS"]intValue];
    NSString *phone = [userDefaults objectForKey:@"NOW_USER_PHONE"];
    
        //根据状态区分不同的按钮状态
    if(status){
        
        NSString *name = [userDefaults objectForKey:[@"name="stringByAppendingString:phone]];
//
        NSString *title = name ? name : @"昵称";
        
//        NSString *title = @"昵称";
        
        [_headerView.loginButton setTitle:phone forState:UIControlStateNormal];
        [_headerView.registerButton setTitle:title forState:UIControlStateNormal];
        _headerView.loginButton.enabled = NO;
        _headerView.registerButton.enabled = NO;
        
    }else{
    
        [_headerView.loginButton setTitle:@"登录" forState:UIControlStateNormal];
        [_headerView.registerButton setTitle:@"注册" forState:UIControlStateNormal];
        _headerView.loginButton.enabled = YES;
        _headerView.registerButton.enabled = YES;
        
    }
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self nowArray].count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"leftTableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    [cell.imageView sizeThatFits:CGSizeMake(20, 20)];
    
    
    NSArray *data = [self nowArray];
    
    cell.imageView.image = [UIImage imageNamed:data[indexPath.row][@"img"]];
    cell.textLabel.text = data[indexPath.row][@"name"];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dict = [self nowArray][indexPath.row];
    
    
    NKMainNavigation *nav = (NKMainNavigation *)self.sideBarController.contentViewController;
        //NKHomeViewController 跳至特殊(包含TabBar)控制器
    if ([dict[@"className"] isEqualToString:@"NKHomeViewController"]) {
        
        [nav showHomeViewController];
        nav.title = dict[@"name"];
        
    }else{
        
            //show一个控制器
        [nav showOtherViewController:dict[@"className"] title:dict[@"name"]];
        
    }

    [self.sideBarController hideMenuViewController:YES];
    
}



#pragma mark - 加载本地数据源
-(void)loadData{
    
    NSArray *nameArray = @[@"首页",@"医生详情",@"病历管理",@"关注医生",@"设置与帮助",@"用户协议"];
    NSArray *classArray = @[@"NKHomeViewController",@"NKDoctorDetailsViewController",@"NKDiagnosisViewController",@"NKFindViewController",@"NKSettingViewController",@"NKTreatyViewController"];
    NSArray *img = @[@"lefts_1",@"lefts_2",@"lefts_3",@"lefts_4",@"lefts_5",@"lefts_6"];
    
    NSMutableArray *dictArray = [NSMutableArray array];
    [nameArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:obj,@"name",classArray[idx],@"className",img[idx],@"img",nil];
        
        [dictArray addObject:dict];
        
    }];
    
    _menuArray = [dictArray copy];
    
        //设置默认数据
    NSMutableArray *normalArrayM = [NSMutableArray array];
    [normalArrayM addObject:dictArray[0]];
    [normalArrayM addObject:dictArray[4]];
    [normalArrayM addObject:dictArray[5]];
    _normalArray = [normalArrayM copy];
    
}


/**
 登录状态

 @return YES登录,NO未登录
 */
-(BOOL)isLogin{

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSInteger status = [[userDefaults objectForKey:@"USER_STATUS"]intValue];
    
    return status;
}



/**
 当前数据源

 @return 当前所需数据源
 */
-(NSArray *)nowArray{
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSInteger status = [[userDefaults objectForKey:@"USER_STATUS"]intValue];
    NSArray *data = status ? _menuArray : _normalArray;
    return data;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
